﻿using DualReferralClient.Entity;
using DualReferralClient.Model;
using log4net;
using MetroFramework.Forms;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace DualReferralClient.View
{
    public partial class MainForm : MetroForm
    {
        private DualReferralModel dualReferralClient = new DualReferralModel();
        ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        List<PatientList> list = new List<PatientList>();
        PatientList patientList = new PatientList();

        public MainForm()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;//无边框
            DataGridView.DataSource = dualReferralClient.getPatientList();
            list = dualReferralClient.getPatientList();
            patientList = list[0];
        }

        private void SerchButton_Click(object sender, System.EventArgs e)
        {
            DataGridView.DataSource = dualReferralClient.getPatientList("%" + ConditionTextBox.Text);
            list = dualReferralClient.getPatientList();
        }

        private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(list[DataGridView.SelectedRows[0].Index].ToString());
            patientList = list[DataGridView.SelectedRows[0].Index];
        }

        //转诊申请
        private void ToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);

            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("referral");
            xmldoc.AppendChild(xmlRoot);
            //发起转诊编码
            XmlElement orderid = xmldoc.CreateElement("orderid");
            XmlText orderidText = xmldoc.CreateTextNode(GetTimeStamp(DateTime.Now));
            orderid.AppendChild(orderidText);
            xmlRoot.AppendChild(orderid);
            //发起医院代码
            XmlElement fqyydm = xmldoc.CreateElement("fqyydm");
            XmlText fqyydmText = xmldoc.CreateTextNode("43120477700");
            fqyydm.AppendChild(fqyydmText);
            xmlRoot.AppendChild(fqyydm);
            //发起医院名称
            XmlElement fqyymc = xmldoc.CreateElement("fqyymc");
            XmlText fqyymcText = xmldoc.CreateTextNode("云南省第三人民医院");
            fqyymc.AppendChild(fqyymcText);
            xmlRoot.AppendChild(fqyymc);
            //发起科室代码
            XmlElement fqksdm = xmldoc.CreateElement("fqksdm");
            XmlText fqksdmText = xmldoc.CreateTextNode(GlobalVar.KSCode);
            fqksdm.AppendChild(fqksdmText);
            xmlRoot.AppendChild(fqksdm);
            //发起科室名称
            XmlElement fqksmc = xmldoc.CreateElement("fqksmc");
            XmlText fqksmcText = xmldoc.CreateTextNode(GlobalVar.KSName);
            fqksmc.AppendChild(fqksmcText);
            xmlRoot.AppendChild(fqksmcText);
            //发起医生工号
            XmlElement fqysgh = xmldoc.CreateElement("fqysgh");
            XmlText fqysghText = xmldoc.CreateTextNode(GlobalVar.YSCode);
            fqysgh.AppendChild(fqysghText);
            xmlRoot.AppendChild(fqysgh);
            //发起医生姓名
            XmlElement fqysxm = xmldoc.CreateElement("fqysxm");
            XmlText fqysxmText = xmldoc.CreateTextNode(GlobalVar.YSName);
            fqysxm.AppendChild(fqysxmText);
            xmlRoot.AppendChild(fqysgh);
            //token
            XmlElement token = xmldoc.CreateElement("token");
            XmlText tokenText = xmldoc.CreateTextNode("");
            token.AppendChild(tokenText);
            xmlRoot.AppendChild(token);
            //病人类型
            XmlElement brlx = xmldoc.CreateElement("brlx");
            XmlText brlxText = xmldoc.CreateTextNode("0");
            brlx.AppendChild(brlxText);
            xmlRoot.AppendChild(brlx);
            //患者姓名
            XmlElement hzxm = xmldoc.CreateElement("hzxm");
            XmlText hzxmText = xmldoc.CreateTextNode(patientList.hzxm);
            hzxm.AppendChild(hzxmText);
            xmlRoot.AppendChild(hzxm);
            //就诊卡号
            XmlElement jzkh = xmldoc.CreateElement("jzkh");
            XmlText jzkhText = xmldoc.CreateTextNode(patientList.jzkh);
            jzkh.AppendChild(jzkhText);
            xmlRoot.AppendChild(jzkh);
            //就诊卡类型
            XmlElement jzklx = xmldoc.CreateElement("jzklx");
            XmlText jzklxText = xmldoc.CreateTextNode(patientList.jzklx);
            jzklx.AppendChild(jzklxText);
            xmlRoot.AppendChild(jzklx);
            //身份证号码
            XmlElement sfzh = xmldoc.CreateElement("sfzh");
            XmlText sfzhText = xmldoc.CreateTextNode(patientList.sfzh);
            sfzh.AppendChild(sfzhText);
            xmlRoot.AppendChild(sfzhText);
            //出生日期
            XmlElement csrq = xmldoc.CreateElement("csrq");
            XmlText csrqText = xmldoc.CreateTextNode(patientList.csrq);
            csrq.AppendChild(csrqText);
            xmlRoot.AppendChild(csrq);
            //联系电话
            XmlElement lxdh = xmldoc.CreateElement("lxdh");
            XmlText lxdhText = xmldoc.CreateTextNode(patientList.lxdh);
            lxdh.AppendChild(lxdhText);
            xmlRoot.AppendChild(lxdh);
            //家庭地址
            XmlElement jtdz = xmldoc.CreateElement("jtdz");
            XmlText jtdzText = xmldoc.CreateTextNode(patientList.jtdz);
            jtdz.AppendChild(jtdzText);
            xmlRoot.AppendChild(jtdz);
            //初步诊断
            XmlElement cbzd = xmldoc.CreateElement("cbzd");
            XmlText cbzdText = xmldoc.CreateTextNode(patientList.cbzd);
            cbzd.AppendChild(cbzdText);
            xmlRoot.AppendChild(cbzd);
            //病史摘要
            XmlElement bszy = xmldoc.CreateElement("bszy");
            XmlText bszyText = xmldoc.CreateTextNode(patientList.bszy);
            bszy.AppendChild(bszyText);
            xmlRoot.AppendChild(bszy);
            //治疗情况
            XmlElement zyjws = xmldoc.CreateElement("zyjws");
            XmlText zyjwsText = xmldoc.CreateTextNode(patientList.zyjws);
            zyjws.AppendChild(zyjwsText);
            xmlRoot.AppendChild(zyjws);
            //转诊目的
            XmlElement zzmd = xmldoc.CreateElement("zzmd");
            XmlText zzmdText = xmldoc.CreateTextNode(patientList.zzmd);
            zzmd.AppendChild(zzmdText);
            xmlRoot.AppendChild(zzmd);
            //紧急程度
            XmlElement jjcd = xmldoc.CreateElement("jjcd");
            XmlText jjcdText = xmldoc.CreateTextNode("2");
            jjcd.AppendChild(jjcdText);
            xmlRoot.AppendChild(jjcd);
            //门诊住院标志
            XmlElement mzzycxbz = xmldoc.CreateElement("mzzycxbz");
            XmlText mzzycxbzText = xmldoc.CreateTextNode(GlobalVar.type);
            mzzycxbz.AppendChild(mzzycxbzText);
            xmlRoot.AppendChild(mzzycxbz);
            MessageBox.Show(xmldoc.InnerXml);
            WebPost("reservation", xmldoc.InnerXml, "系统发起转诊申请", 1);
        }

        //转诊单查询
        private void SerchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);


            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("referral-query");
            xmldoc.AppendChild(xmlRoot);
            //医院代码
            XmlElement yydm = xmldoc.CreateElement("yydm");
            XmlText yydmText = xmldoc.CreateTextNode("43120477700");
            yydm.AppendChild(yydmText);
            xmlRoot.AppendChild(yydm);
            //医生工号
            XmlElement ysgh = xmldoc.CreateElement("ysgh");
            XmlText ysghText = xmldoc.CreateTextNode(GlobalVar.YSCode);
            ysgh.AppendChild(ysghText);
            xmlRoot.AppendChild(ysgh);
            //患者姓名
            XmlElement hzxm = xmldoc.CreateElement("hzxm");
            XmlText hzxmText = xmldoc.CreateTextNode(patientList.hzxm);
            hzxm.AppendChild(hzxmText);
            xmlRoot.AppendChild(hzxm);
            //就诊卡号
            XmlElement jzkh = xmldoc.CreateElement("jzkh");
            XmlText jzkhText = xmldoc.CreateTextNode(patientList.jzkh);
            jzkh.AppendChild(jzkhText);
            xmlRoot.AppendChild(jzkh);
            //身份证号
            XmlElement sfzh = xmldoc.CreateElement("sfzh");
            XmlText sfzhText = xmldoc.CreateTextNode(patientList.sfzh);
            sfzh.AppendChild(sfzhText);
            xmlRoot.AppendChild(sfzh);

            //MessageBox.Show(xmldoc.InnerXml);
            WebPost("SxzzAppServlet", xmldoc.InnerXml, "转诊单查询", 2);

        }

        //数据上传网络请求
        private void WebPost(string RequestMethodName, string xml, string LogStr, int flag)
        {

            //上传数据
            string url = ConfigurationManager.ConnectionStrings["url"].ConnectionString;
            RestClient client = new RestClient(url);
            //参数1为服务名称
            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("method", RequestMethodName);
            request.AddParameter("application/xml", xml, ParameterType.RequestBody);
            //执行请求并返回结果
            IRestResponse response = client.Execute(request);
            string param, result = "",message="";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(response.Content.ToString());
            if (response.IsSuccessful)
            {
                switch (flag)
                {
                    case 1:
                        XmlNode root = xmlDoc.SelectSingleNode("referral");
                        //获取到所有的子节点
                        XmlNodeList nodeList = root.ChildNodes;
                        foreach (XmlNode xn in nodeList)
                        {
                            if ("parm".Equals(xn.Name))
                            {
                                param = xn.InnerText;
                                GlobalVar.param = param;
                            }

                            if ("result".Equals(xn.Name))
                            {
                                result = xn.InnerText;
                            }
                        }
                        //跳转页面前的判断
                        if (result.Equals("true"))
                        {
                            GlobalVar.pageController = ConfigurationManager.ConnectionStrings["resultUrl"].ConnectionString;
                            WebForm webForm = new WebForm();
                            webForm.StartPosition = FormStartPosition.CenterParent;
                            webForm.ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show("输入参数有误");
                        }
                        break;

                    case 2:
                        XmlNode root1 = xmlDoc.SelectSingleNode("referral-query");
                        //获取到所有的子节点
                        XmlNodeList nodeListAll = root1.ChildNodes;
                        foreach (XmlNode xn in nodeListAll)
                        {
                            if ("parm".Equals(xn.Name))
                            {
                                param = xn.InnerText;
                                GlobalVar.param = param;
                            }

                            if ("result".Equals(xn.Name))
                            {
                                result = xn.InnerText;
                            }

                            if ("message".Equals(xn.Name))
                            {
                                message = xn.InnerText;
                            }
                        }
                        //跳转页面前的判断
                        if (result.Equals("true"))
                        {
                            GlobalVar.pageController = ConfigurationManager.ConnectionStrings["searchUrl"].ConnectionString;
                            WebForm webForm = new WebForm();
                            webForm.StartPosition = FormStartPosition.CenterParent;
                            webForm.ShowDialog();
                        }
                        else
                        {
                            MessageBox.Show(message);
                        }
                        break;
                }

            }
            else
            {
                log.Error(DateTime.Now.ToString() + " " + RequestMethodName + " " + "发送请求失败");
                MessageBox.Show("发送请求失败");
            }
        }

        //唯一码生成
        private string GetTimeStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;
            return t + GlobalVar.GetRandom();
        }
    }
}
